package com.kdl.webconfig.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kdl.webconfig.converter.VOConvert;
import com.kdl.webconfig.vo.BasePageVO;
import com.kdl.webconfig.vo.PageVO;
import com.kdl.webconfig.converter.PojoConvert;
import com.kdl.webconfig.entity.CustomInfo;
import com.kdl.webconfig.service.ICustomInfoService;
import com.kdl.webconfig.utils.DateUtils;
import com.kdl.webconfig.vo.config.CustomInfoVO;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author 王成杰
 * @description TODO
 * @date 2022-09-06 上午11:48
 */
@Component
public class CustomBusiness {

    @Autowired
    ICustomInfoService iCustomInfoService;


    public PageVO<CustomInfoVO> page(BasePageVO basePageVO) {
        Page<CustomInfo> page = iCustomInfoService.queryPage(basePageVO);
        return buildCustomInfoPage(page);
    }

    private PageVO<CustomInfoVO> buildCustomInfoPage(Page<CustomInfo> page) {
        PageVO<CustomInfoVO> newsVOPageVO = new PageVO<>();
        if(Objects.nonNull(page)) {
            newsVOPageVO.setCurrentPage(page.getCurrent());
            newsVOPageVO.setPageSize(page.getSize());
            newsVOPageVO.setTotalPages(page.getPages());
            newsVOPageVO.setTotalSize(page.getTotal());
            newsVOPageVO.setList(VOConvert.parseCustomInfoVOSByPojos(page.getRecords()));
        }
        return newsVOPageVO;
    }


    /**
     *  删除客户
     * @param id
     * @return
     */
    public Boolean deleteById(Integer id) {
        Integer integer = iCustomInfoService.deleteById(id);
        return integer>0;
    }

    /**
     * 用户新增
     * @param customInfoVO
     * @return
     */
    public boolean save(CustomInfoVO customInfoVO) {
        CustomInfo customInfo = PojoConvert.parseCustomInfo(customInfoVO);
        return iCustomInfoService.save(customInfo);
    }

    /**
     * excel导出
     * @return
     */
    public XSSFWorkbook formGeneration() {
        List<CustomInfo> customInfoList = iCustomInfoService.selectCustomInfo();
        XSSFWorkbook xssfSheets = new XSSFWorkbook();

        XSSFSheet userList = xssfSheets.createSheet("customInfoList");

        Row titleRow = userList.createRow(0);//创建第一行，起始为0
        titleRow.createCell(0).setCellValue("id");//第一列
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("手机");
        titleRow.createCell(3).setCellValue("邮箱");
        titleRow.createCell(4).setCellValue("留言");
        titleRow.createCell(5).setCellValue("创建时间");
        titleRow.createCell(6).setCellValue("是否删除");

        for (int i = 0; i < customInfoList.size(); i++) {
            Row row = userList.createRow(i + 1);//设置对哪一行操作
            row.createCell(0).setCellValue(customInfoList.get(i).getId());
            row.createCell(1).setCellValue(customInfoList.get(i).getCustomName());
            row.createCell(2).setCellValue(customInfoList.get(i).getCustomPhone());
            row.createCell(3).setCellValue(customInfoList.get(i).getCustomEmail());
            row.createCell(4).setCellValue(customInfoList.get(i).getMessage());
            row.createCell(5).setCellValue(DateUtils.formatTime(customInfoList.get(i).getCreateTime()));
            row.createCell(6).setCellValue(customInfoList.get(i).getDeleteFlag());
        }

        return xssfSheets;
    }
}
